
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FactoryDefaultFrame extends JFrame{


    SQLOperation sql=new SQLOperation();

    public FactoryDefaultFrame(String mUID){

        setTitle("System Management Operation");
        setLayout(null);




        JLabel title=new JLabel("  SmartHome Management System");
        title.setFont(new Font("",Font.BOLD,60));
        title.setBounds(0,0,1000,125);
        add(title);


        JButton logout=new JButton("Log out");
        logout.setFont(new Font("",Font.BOLD,25));

        logout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int exitOperation=JOptionPane.showConfirmDialog
                        (null, "Confirm log out?","Confirm",JOptionPane.YES_NO_OPTION);

                if(exitOperation==JOptionPane.YES_OPTION){
                    setVisible(false);
                    new Login();
                }

            }
        });


        logout.setBounds(1030,25,140,75);
        add(logout);



        String[] deviceAttributes={"idDevice","Family_familyID","DeviceType_typeID"};
        String[] accountAttributes={"tName"};
        String[] attributes={"DeviceID","familyID","typeID","typeName"};
        Object[][] deviceDB=sql.search("Device",deviceAttributes,"Manufacture_mUID",mUID);


        int pass=0;
        for(int i=0;i< deviceDB.length;i++) {
            if (deviceDB[i][1].equals("0") || deviceDB[i][1].equals("")) {
                pass++;
            }
        }
        Object[][] deviceList=new Object[deviceDB.length-pass][4];
        pass=0;
        for(int i=0;i< deviceDB.length;i++){
            if(deviceDB[i][1].equals("0")||deviceDB[i][1].equals("")){
                pass++;
            }
            else {
                for (int j = 0; j <3; j++) deviceList[i-pass][j] = deviceDB[i][j];
                deviceList[i-pass][3] = sql.search("DeviceType", accountAttributes, "typeID", deviceDB[i][2])[0][0];

            }
        }








        JTable deviceTable=new JTable(deviceList,attributes);
        deviceTable.setRowHeight(30);
        deviceTable.setFont(new Font("",Font.PLAIN,15));

        deviceTable.setEnabled(true);
        deviceTable.getTableHeader().setFont(new Font("",Font.BOLD,20));
        JScrollPane scrollPane=new JScrollPane(deviceTable);
        scrollPane.setBounds(0,125,1200,500);
        add(scrollPane);

        JButton equipAdd=new JButton("Equip Add");
        equipAdd.setFont(new Font("",Font.BOLD,25));
        equipAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FactoryAdd(mUID);

            }
        });
        equipAdd.setBounds(50,650,200,75);
        add(equipAdd);


        JButton equipChange=new JButton("Equip Modify");
        equipChange.setFont(new Font("",Font.BOLD,25));
        equipChange.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FactoryModify(mUID);

            }
        });
        equipChange.setBounds(350,650,200,75);
        add(equipChange);


        JButton equipData=new JButton("Equip Data");
        equipData.setFont(new Font("",Font.BOLD,25));
        equipData.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FactoryData(mUID);

            }
        });
        equipData.setBounds(650,650,200,75);
        add(equipData);

        JButton listOne=new JButton("List an Equip");
        listOne.setFont(new Font("",Font.BOLD,25));
        listOne.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FactoryListEquip(mUID);

            }
        });
        listOne.setBounds(950,650,200,75);
        add(listOne);




        setSize(1200,800);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);

    }
}
